package com.cn.main.framework;

import com.cn.main.framework.proxy.RunFactoryBean;
import com.cn.main.framework.scanner.ProxyInterfaceBeanBeanDefinitionScanner;
import com.cn.main.framework.test.Bird;
import lombok.SneakyThrows;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @aothor chenyuncong
 * @Date 2022/9/4 上午9:51
 */
public class RunFactoryBeanDefinitionRegistry implements ImportBeanDefinitionRegistrar {
    @SneakyThrows
    @Override
    public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
                                        BeanDefinitionRegistry registry) {

        ProxyInterfaceBeanBeanDefinitionScanner scanner = new ProxyInterfaceBeanBeanDefinitionScanner(registry);
//        scanner.scan(getBasePackages(annotationMetadata));

        Class<?> beanClazz= Bird.class;
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(beanClazz);
        GenericBeanDefinition definition = (GenericBeanDefinition) builder.getRawBeanDefinition();

        MutablePropertyValues propertyValues = definition.getPropertyValues();
        propertyValues.add("interfaceType", beanClazz);
        definition.setBeanClass(RunFactoryBean.class);
        definition.setAutowireMode(GenericBeanDefinition.AUTOWIRE_BY_TYPE);
        registry.registerBeanDefinition(beanClazz.getSimpleName(), definition);
    }
}
